import 'dart:convert';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:http/http.dart' as http;
import 'package:intl/intl.dart';

import '../Robot.dart';
import '../details.dart';
import '../pojo.dart';

class CustomersProvider extends ChangeNotifier {
  // Customer information fields
  String _customerName = "";
  String _phoneNumber = "";
  String _customerType = "";
  int _renderCount = 19;
  double _totalAmount = 2000.00;

  // Getter methods for each property
  String get customerName => _customerName;
  String get phoneNumber => _phoneNumber;
  String get customerType => _customerType;
  int get renderCount => _renderCount;
  double get totalAmount => _totalAmount;



  //  客户列表
  List<Customer> _list = [];

  List<Customer> get customerList => _list;

  //  客户渲染详情
  List<Task> _list2 = [];
  bool _isLoading = false;
  bool get isLoading => _isLoading;
  final String name = "name";

  List<Item147> dataList2 = [];
//界面只需要这个
  List<Item147> get list3 => dataList2;


  Future<void> getCustomers({
    String name = '', // Default value is an empty string
    String phone = '', // Default value is an empty string
      }
      ) async {
    print("getCompanyProduct start");
    _isLoading = true;
    notifyListeners();
    final accessToken = Robot.accessToken;
    final userId = Robot.userId;
    final companyId = Robot.companyId;

    // 创建 Condition3 对象
    Condition3 condition = Condition3(customerName:name,customerPhone: phone);

    // 创建 FetchDataRequest9 对象
    FetchDataRequest9<Condition3> request = FetchDataRequest9<Condition3>(
      condition: condition,
    );

    var url = Uri.https(host, 'userOrderCustomer/page');
    final uri = url.replace(
      queryParameters: {
        "access_token": accessToken,
        "userId": userId,
        "companyId": companyId,
      },
    );

    final a = jsonEncode(request.toJson((condition) => condition.toJson()));
    // print("toJson: ${a}");
    final response = await http.post(
      uri,
      headers: {"Content-Type": "application/json; charset=UTF-8"},
      body: a,
    );

    if (response.statusCode == 200) {
      print("response：${response.body}");
      final Map<String, dynamic> jsonResponse = json.decode(response.body);
      _isLoading = false;
      // print("getCompanyProduct end");
      var a = ApiResponse<Data9>.fromJson(
        jsonResponse,
        (data) => Data9.fromJson(data),
      );
      _list = a.data!.list;
      notifyListeners();
      return;
    } else {
      throw Exception('Failed to fetch products: ${response.reasonPhrase}');
    }
  }

  Future<bool> login() async {
    print("login start");
    var url = Uri.https(host, 'robot/login');
    var response = await http.post(url, body: {'sn': Robot.sn});
    var a = ApiResponse<LoginBean>.fromJson(
      jsonDecode(response.body),
      (data) => LoginBean.fromJson(data),
    ); // 传递一个从 JSON 解析 LoginBean 的方法);

    print(a.data.toString());
    if (a.data?.userId == null) {
      print('请扫码');
      return false;
    }
    Robot.userId = a.data!.userId!;
    Robot.companyId = a.data!.companyId!;
    Robot.accessToken = a.data!.accessToken!;
    print("Robot.accessToken:" + Robot.accessToken);
    print("login end");
    return true;
  }
}

const fetchSize = 24;
